﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MMOG
{
    public partial class LobbyForm : Form
    {
        public LobbyForm()
        {
            InitializeComponent();
        }

        private void roomRefreshTimer_Tick(object sender, EventArgs e)
        {
            Information info = new Information(Information.REQUEST_ROOM_LIST);
            lock (Client.writer)
            {
                Client.writer.WriteLine(info.ToString());
                Client.writer.Flush();
            }
            refreshButton.Enabled = true;
        }

        private void roomControl1_Load(object sender, EventArgs e)
        {

        }

        private void createButton_Click(object sender, EventArgs e)
        {
            CreateRoomForm roomForm = new CreateRoomForm();
            roomForm.gameSelect.SelectedIndex = 0;
            roomForm.gameSelect.Enabled = false;
            roomForm.ShowDialog(this);
            String name="", password="";

            bool created = roomForm.accepted;
            if (created)
            {                
                Information info = new Information(Information.CREATE_ROOM);
                name = roomForm.roomNameField.Text;
                password = roomForm.passwordField.Text;
                info.add("name", name);
                info.add("nPlayer", "1");
                info.add("password", password);
                lock (Client.writer)
                {
                    Client.writer.WriteLine(info.ToString());
                    Client.writer.Flush();
                }
            }
            roomForm.Dispose();
            roomForm = null;

            if (created)
            {
                Client.lobbyForm.Hide();
                Client.lobbyForm.roomRefreshTimer.Stop();
                WaitingForm waitForm = new WaitingForm();
                waitForm.Show();
                //waitForm.roomLabel = 
                if (password.CompareTo("") == 0) waitForm.lockIcon.Visible = false;
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            Information info = new Information(Information.REQUEST_ROOM_LIST);
            lock (Client.writer)
            {
                Client.writer.WriteLine(info.ToString());
                Client.writer.Flush();
            }
            refreshButton.Enabled = false;
        }

        private void LobbyForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
            roomRefreshTimer.Stop();
        }
    }
}
